import Taro from '@tarojs/taro'
import $http from '@/http'

import {
  LOGIN,
  REGISTER
} from '../constants/user'


// 登录
export function login(params) {
  return async(dispatch) => {
    Taro.showLoading({
      title: '登录中',
      mask: true
    })

    await $http.get('/juejin/user_api/v1/author/recommend', {
      category_id: '',
      cursor: 0,
      limit: 20,
    })

    dispatch({
      type: LOGIN,
      payload: params
    })

    Taro.hideLoading()
    
    Taro.showToast({
      title: '登录成功',
      icon: 'success',
      mask: true,
      duration: 1500,
      success() {
        console.log('登录成功，即将跳转！')
        // Taro.switchTab()
        Taro.redirectTo({
          url: '/pages/index/index'
        })
      }
    })
  }
}

// 注册
export function register(params) {
  return {
    type: REGISTER,
    payload: params
  } 
}
